package org.luckyjourney.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.luckyjourney.entity.Captcha;
import org.luckyjourney.entity.consultants.TalConsultants;
import org.luckyjourney.entity.consultants.dto.TalConsultantsDTO;
import org.luckyjourney.entity.user.AuthenticationDTO;
import org.luckyjourney.entity.user.User;
import org.luckyjourney.entity.user.dto.UserDTO;
import org.luckyjourney.entity.vo.FindPWVO;
import org.luckyjourney.entity.vo.RegisterVO;
import org.luckyjourney.exception.BaseException;
import org.luckyjourney.service.LoginService;
import org.luckyjourney.service.user.UserRoleService;
import org.luckyjourney.util.JwtUtils;
import org.luckyjourney.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @Author: Xhy
 * @CreateTime: 2023-10-24 16:29
 */

@RestController
@RequestMapping("/luckyjourney/login")
@Api(tags = "普通用户")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    UserRoleService userRoleService;
    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping
    @ApiOperation("普通用户登录")
    public R login(@RequestBody @Validated User user){
        user = loginService.login(user);
        List<String> roleName =userRoleService.selectRoleName(user.getId());
        for (String role : roleName) {
            if (!role.equals("普通用户")) {
                throw new BaseException("该账户没有权限登录");
            }
        }
        // 登录成功，生成token
        String token = JwtUtils.getJwtToken(user.getId(), user.getNickName());
        final HashMap<Object, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("name",user.getNickName());
        return R.ok().data(map);
    }

    /**
     * 登录
     * @param userDTO
     * @return
     */
    @PostMapping("/phoneLogin")
    @ApiOperation("普通用户手机号登录")
    public R PhoneLogin(@RequestBody @Validated UserDTO userDTO) throws Exception {
        User user  = loginService.PhoneLogin(userDTO);
        List<String> roleName =userRoleService.selectRoleName(user.getId());
     if (ObjectUtil.isEmpty(roleName)){
        throw new BaseException("该账户没没有角色");
     }
        for (String role : roleName) {
            if (!role.equals("普通用户")) {
                throw new BaseException("该账户没有权限登录");
            }
        }
        // 登录成功，生成token
        String token = JwtUtils.getJwtToken(user.getId(), user.getNickName());
        final HashMap<Object, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("name",user.getNickName());
        return R.ok().data(map);
    }



    /**
     * 获取图形验证码
     * @param response
     * @param uuId
     * @throws IOException
     */
    @GetMapping("/captcha.jpg/{uuId}")
    @ApiOperation("获取图形验证码")
    public void captcha(HttpServletResponse response, @PathVariable String uuId) throws IOException {
        loginService.captcha(uuId,response);
    }


    /**
     * 获取验证码
     * @param captcha
     * @return
     * @throws Exception
     */
    @PostMapping("/getCode")
    // @ApiOperation("获取验证码")
    public R getCode(@RequestBody @Validated Captcha captcha) throws Exception {
        if (!loginService.getCode(captcha)) {
            return R.error().message("验证码错误");
        }
        return R.ok().message("发送成功,请耐心等待");
    }


    /**
     * 检测邮箱验证码
     * @param email
     * @param code
     * @return
     */
    @PostMapping("/check")
    public R check(String email,Integer code){
        loginService.checkCode(email,code);
        return R.ok().message("验证成功");
    }

    /**
     * 注册
     * @param registerVO
     * @return
     * @throws Exception
     */
    @PostMapping("/register")
    @ApiOperation("普通用户注册")
    public R register(@RequestBody @Validated RegisterVO registerVO) throws Exception {
        if (!loginService.register(registerVO)) {
            return R.error().message("注册失败,验证码错误");
        }
        return R.ok().message("注册成功");
    }

    /**
     * 找回密码
     * @param findPWVO
     * @return
     */
    @PostMapping("/findPassword")
    public R findPassword(@RequestBody @Validated FindPWVO findPWVO,HttpServletResponse response){
        final Boolean b = loginService.findPassword(findPWVO);
        return R.ok().message(b ? "修改成功" : "修改失败,验证码不正确");
    }

}
